package com.gtt.jr.action.admin;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import com.gtt.jr.action.base.BaseModelAction;
import com.gtt.jr.action.model.ContainerAddModel;
import com.gtt.jr.app.ContainerRepository;
import com.gtt.jr.app.domain.ContainerVO;
import com.gtt.jr.util.ImageUtils;

/**
 * Container Edit Exec Action
 * 
 * @author Michael(gao12581@sina.com)
 * @date 2011-3-9 13:07:07
 * 
 */
public class ContainerEditExecAction extends BaseModelAction<ContainerAddModel> {

	private static final long serialVersionUID = -483725174395850649L;

	@Resource
	private ContainerRepository containerRepository;

	@Override
	public String execute() throws Exception {
		containerRepository.modify(convert(getModel()));
		return JSON;
	}

	private ContainerVO convert(ContainerAddModel model) throws IOException {
		ContainerVO vo = new ContainerVO();
		vo.setId(model.getId());
		vo.setType(model.getType());
		vo.setName(model.getName());
		vo.setDescription(model.getDescription());
		if (model.getUpload() != null) {
			Map<String, String> picture = ImageUtils.savePicture(model
					.getUpload(), model.getSuffix(), getRealPath());
			vo.setUrl(picture.get("url"));
			vo.setSurl(picture.get("surl"));
			vo.setTurl(picture.get("turl"));
		}
		return vo;
	}
}
